<!--
 * @Author: 郭安东
 * @Date: 2021-03-12 16:51:04
 * @LastEditTime: 2021-04-11 19:14:15
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \test\src\views\function\temperature.vue
-->
<template>
  <div class="temperature">
    <el-card class="box-card">
      <el-link class="userName">{{ userName }}</el-link>
      <el-tag :type="statesType" style="margin-left: 30px">{{ states }}</el-tag>
      <span>
        <el-button class="loginOut-btn" @click="dialog = true"><i class="el-icon-switch-button"></i></el-button>
      </span>
    </el-card>
    <four-menu :buts-data="dataFourMenu" :title=" '行程管理' " class="four-menu"/>
    <el-card class="box-card">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item>
          <el-button @click="centerDialogVisible = true" v-show=" role === 'role_user'">
            增加
          </el-button>
        </el-form-item>
        <el-form-item label="按用户名查询：" v-show="role === 'role_admin'">
          <el-input placeholder="请输入内容" v-model="searchForm.userName" class="searchInput"></el-input>
        </el-form-item>
        <el-form-item label="出发地：">
          <el-input placeholder="请输入内容" v-model="searchForm.startPlace" class="searchInput"></el-input>
        </el-form-item>
        <el-form-item label="开始时间：">
          <el-date-picker
            v-model="searchForm.startTime"
            type="datetime"
            placeholder="日期"
            class="searchInput">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间：">
          <el-date-picker
            v-model="searchForm.endTime"
            type="datetime"
            placeholder="日期"
            class="searchInput">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" round @click="searchTrip()">搜索</el-button>
          <el-button round @click="getQueryRoutes()">重置</el-button>
          <el-button v-show=" role === 'role_admin'"><el-link href="http://42.193.131.98:8080/exportRouteExcel" target="_blank">
        导出行程表
      </el-link></el-button>
        </el-form-item>
      </el-form>
      <common-table
        ref="mainTableRef"
        :table-data="mainTableData"
        :table-label="mainTableLabel"
        :table-config="mainTableConfig"
        :default-sort="mainTableConfig.defaultSort"
        style="height: 480px; margin-top: 10px"
        @handle-size-change="handleSizeChange"
        @handle-current-change="handleCurrentChange">
        <template slot="operating" slot-scope="scope">
          <el-button class="operatingButton" v-show=" role === 'role_admin'" @click="deleteUserRoute(scope.row)">
            删除
          </el-button>
        </template>
      </common-table>
    </el-card>
    <el-dialog
      title="增加行程信息"
      :visible.sync="centerDialogVisible"
      width="30%"
      center>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户ID">
          <el-input v-model="form.userId" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="form.userName" disabled></el-input>
        </el-form-item>
        <el-form-item label="起始地" required="true">
          <el-input v-model="form.startPlace"></el-input>
        </el-form-item>
        <el-form-item label="结束地" required="true">
          <el-input v-model="form.endPlace"></el-input>
        </el-form-item>
        <el-form-item label="起始时间" required="true">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" required="true">
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="交通工具" required="true">
          <el-input v-model="form.vehicle"></el-input>
        </el-form-item>
        <el-form-item label="座次号" required="true">
          <el-input v-model="form.vehicleSeatNumber"></el-input>
        </el-form-item>
        <el-form-item class="form-btn">
          <el-button type="primary" @click="insertUserRoute()">保存</el-button>
          <el-button @click="centerDialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog
      title="确定注销用户吗"
      :visible.sync="dialog"
      width="30%"
      center>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="loginOut()">确 定</el-button>
        <el-button @click="dialog = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import commonTable from '../../components/common-table/index'
import FourMenu from '../../components/menu/four-menu'
import { userLoginOut, queryRoutes, insertRoute, deleteRoute } from '../../api/apply-info'
export default {
  name: 'Trip',
  components: { commonTable, FourMenu },
  data () {
    return {
      centerDialogVisible: false,
      dialog: false,
      userName: '',
      userId: '',
      role: '',
      states: '',
      statesType: '',
      form: {
        userId: '',
        userName: '',
        startPlace: '',
        endPlace: '',
        startTime: '',
        endTime: '',
        vehicle: '',
        vehicleSeatNumber: ''
      },
      searchForm: {
        userId: '',
        userName: '',
        startPlace: '',
        endPlace: '',
        startTime: '',
        endTime: '',
        vehicle: '',
        vehicleSeatNumber: '',
        pageSize: 10,
        currentPage: 1
      },
      searchData: {
        userId: '',
        userName: '',
        startPlace: '',
        startTime: '',
        endTime: '',
        pageSize: 10,
        currentPage: 1
      },
      index: 0,
      dataFourMenu: [{ name: '返回' }],
      mainTableData: [],
      mainTableLabel: [
        {
          prop: 'id',
          label: '编号',
          width: 125,
          sortable: true
        },
        {
          prop: 'userName',
          label: '用户名',
          width: 125,
          sortable: true
        },
        {
          prop: 'startPlace',
          label: '起始地',
          width: 125,
          sortable: true
        },
        {
          prop: 'endPlace',
          label: '结束地',
          width: 125,
          sortable: true
        },
        {
          prop: 'startTime',
          label: '开始时间',
          width: 200,
          sortable: true
        },
        {
          prop: 'endTime',
          label: '结束时间',
          width: 200,
          sortable: true
        },
        {
          prop: 'vehicle',
          label: '交通工具',
          width: 125,
          sortable: true
        },
        {
          prop: 'vehicleSeatNumber',
          label: '座次号',
          width: 125,
          sortable: true
        },
        {
          prop: 'operating',
          label: '操作',
          fixed: 'right',
          width: 150,
          slot: true
        }
      ],
      mainTableConfig: {
        // 是否显示loading效果
        loading: false,
        // 是否开启分页
        pagination: true,
        // 当前页数
        page: 1,
        // 当前页条数
        count: 0,
        // 数据总条数
        totalCount: 0,
        // 每页显示条数
        pageSize: 10,
        // 每页显示条数选择项
        pageSizeOptions: [10, 20, 30, 40],
        // 是否开启隔行换色
        stripe: true,
        // 是否生成序号列
        orderNum: false,
        // 是否生成行选择框列
        rowSelect: true,
        // 表格唯一ID字段
        uniqueId: 'eventNo',
        // 表格高度，低于所有行高时会生成滚动条，高于所有数据的高度会留白
        tableHeight: '90%',
        // 填入表头高度和字体大小的样式对象
        headerSize: {
          height: '40px',
          'font-size': '15px'
        },
        // 填入表头背景色和字体色的样式对象
        headerColor: {
          color: '#333333',
          'background-color': '#e4eefc'
        },
        // 填入表格高度和字体大小的样式对象
        rowSize: {
          height: '35px',
          'font-size': '14px'
        },
        // 填入表格背景色和字体色的样式对象
        rowColor: {
          color: '#333333',
          'background-color': '#f7faff'
        },
        // 是否开启点击选中行
        clickRow: false,
        showTooltip: true
      }
    }
  },
  mounted () {
    this.getUserName()
    this.getQueryRoutes()
  },
  methods: {
    // 获取账户id
    getUserName () {
      this.userName = sessionStorage.getItem('userName')
      this.form.userName = sessionStorage.getItem('userName')
      this.userId = sessionStorage.getItem('userId')
      this.form.userId = sessionStorage.getItem('userId')
      this.role = sessionStorage.getItem('role')
      this.states = sessionStorage.getItem('states')
      if (this.states === '状态正常' || this.states === '管理员') {
        this.statesType = 'success'
      } else {
        this.statesType = 'danger'
      }
    },
    // 注销用户
    loginOut () {
      userLoginOut().then(res => {
        console.log('loginOut', res)
        this.dialog = false
        this.$message(res.data.message)
        // 将用户名放入sessionStorage
        sessionStorage.setItem('userName', null)
        sessionStorage.setItem('userId', null)
        sessionStorage.setItem('role', null)
        // 将用户放入vuex
        this.$store.dispatch('setUser', null)
        this.$router.push({ path: '/login' })
      })
    },
    // 查询行程信息
    getQueryRoutes () {
      if (this.role === 'role_admin') {
        this.searchData.userId = null
      } else if (this.role === 'role_user') {
        this.searchData.userId = this.userId
        this.searchData.userName = this.userName
      }
      queryRoutes(this.searchData).then(res => {
        console.log('queryRoutes res', res)
        this.mainTableData = res.data.data.records
        this.mainTableConfig.totalCount = res.data.data.total
      })
    },
    // 时间戳转换
    getNewTime (time) {
      const year = time.getFullYear()
      let mounth = time.getMonth() + 1
      mounth = mounth < 10 ? ('0' + mounth) : mounth
      let day = time.getDate()
      day = day < 10 ? ('0' + day) : day
      const hour = time.getHours()
      const minute = time.getMinutes()
      const second = time.getSeconds()
      const Time = year + '-' + mounth + '-' + day + ' ' + hour + ':' + minute + ':' + second
      return Time
    },
    // 新增行程信息
    insertUserRoute () {
      if (this.form.startPlace !== '' && this.form.endPlace !== '' && this.form.startTime !== '' && this.form.endTime !== '' && this.form.vehicle !== '' && this.form.vehicleSeatNumber !== '') {
        this.form.startTime = this.getNewTime(this.form.startTime)
        this.form.endTime = this.getNewTime(this.form.endTime)
        insertRoute(this.form).then(res => {
          console.log('insertRoute res', res)
          this.centerDialogVisible = false
          this.getQueryRoutes()
        })
      }
    },
    // 删除行程信息
    deleteUserRoute (row) {
      console.log('row', row)
      deleteRoute(row.id).then(res => {
        this.$message(res.data.message)
        this.getQueryRoutes()
      })
    },
    // 按条件查询
    searchTrip () {
      if (this.searchForm.startTime !== '' && this.searchForm.startTime !== null) {
        this.searchForm.startTime = this.getNewTime(this.searchForm.startTime)
        this.searchForm.endTime = this.getNewTime(this.searchForm.endTime)
      }
      if (this.searchForm.startTime === null) {
        this.searchForm.startTime = ''
        this.searchForm.endTime = ''
      }
      if (this.role === 'role_admin') {
        this.searchForm.userId = null
      } else if (this.role === 'role_user') {
        this.searchForm.userId = this.userId
        this.searchForm.userName = this.userName
      }
      queryRoutes(this.searchForm).then(res => {
        console.log('queryRoutes res', res)
        this.mainTableData = res.data.data.records
      })
    },
    // 每页条数发生改变时触发
    handleSizeChange (val) {
      this.mainTableConfig.page = 1
      this.mainTableConfig.pageSize = val
      this.mainTableData = []
    },
    // 当前页发生改变时触发
    handleCurrentChange (val) {
      this.mainTableConfig.page = val
      this.searchData.currentPage = val
      this.getQueryRoutes()
    }
  }
}
</script>
<style scoped>
.box-card {
    margin-top: 10px;
    margin-left: 10%;
    width: 1200px;
    border-radius: 20px;
    background-color: rgba(255, 255, 255, 0.8);
}
.userName{
  color: #7196b1;
  font-size: 16px;
  font-weight: bolder;
}
.loginOut-btn{
  float: right;
  margin-top: -5px;
  border-radius: 10px;
  color: #7196b1;
  border: none;
  background-color: rgb(240, 234, 234, 0.6);
}
.four-menu {
  margin-top: 10px;
  margin-bottom: 10px;
}
.form-btn {
  margin-left: 15%;
}
.searchInput {
  width: 160px;
}
</style>
<style>
body{
  background-image: url('../../assets/bg3.jpeg');
  background-size: 100% 1000px;
  background-repeat: no-repeat;
}
.el-icon-switch-button {
  color: #e4252d;
  font-size: 17px;
  font-weight: bolder;
}
</style>
